﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreClientModuleEx;
using UnityCore;
using System.Drawing.Printing;
using StoreClientModule;

namespace MBBStoreClient
{
    public partial class frmInventoryViewer : Form
    {
        StockManager sm = new StockManager();
        Threader t = new Threader();
        string lastSearch = "";
        int cPage = 0;
        int cRes = 50;
        public frmInventoryViewer()
        {
            InitializeComponent();

            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);

            sm.ProcessCompleted += new CoreHandler(sm_ProcessCompleted);
            sm.ProcessError += new CoreHandler(sm_ProcessError);
            btnSearch.Click += new EventHandler(SearchProduct);
            btnPrev.Click += new EventHandler(btnPrev_Click);
            btnNext.Click += new EventHandler(btnNext_Click);
            txtSearch.KeyPress += new KeyPressEventHandler(txtSearch_KeyPress);
            btnPrint.Click += new EventHandler(btnPrint_Click);
        }

        void btnPrint_Click(object sender, EventArgs e)
        {
            if (grdResult.Rows.Count > 0)
            {
                PrintInventory();
            }
        }

      
        void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                SearchProduct(sender, e);
            }
        }

        void btnNext_Click(object sender, EventArgs e)
        {
        //    ControlState(false);
        //    cPage++;
        //    GenericSearchType gst = new GenericSearchType() { Keyword = lastSearch, PageNum = cPage, Qty = cRes };
        //    sm.SearchProduct(gst);
        }

        void btnPrev_Click(object sender, EventArgs e)
        {
         //   ControlState(false);
         //   cPage--;
         //   GenericSearchType gst = new GenericSearchType() { Keyword = lastSearch, PageNum = cPage, Qty = cRes };
         //   sm.SearchProduct(gst);
        }

        void SearchProduct(object sender, EventArgs e)
        {
            ControlState(false);
            lastSearch = txtSearch.Text;
            //cPage = 1;
            //GenericSearchType gst = new GenericSearchType(){Keyword = lastSearch, PageNum = cPage, Qty = cRes};
            sm.GetStockInfoForPrinting(lastSearch);
        }

        void sm_ProcessError(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                MessageBox.Show(msg, pcode, MessageBoxButtons.OK, MessageBoxIcon.Error);
                ControlState(true);
            });
        }
        void sm_ProcessCompleted(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                switch (pcode)
                {
                    case "GetStockInfoForPrinting":
                        grdResult.DataSource = sm.StockTable;

                        grdResult.Columns[0].HeaderText = "Barcode";
                        grdResult.Columns[1].HeaderText = "Description";
                        grdResult.Columns[2].HeaderText = "Whole Stock";
                        grdResult.Columns[3].HeaderText = "Retail Stock";
                        grdResult.Columns[4].HeaderText = "PCode";
                        grdResult.Columns[5].HeaderText = "Amount";

                        grdResult.Columns[0].Width = (grdResult.Width * 25) / 100;
                        grdResult.Columns[1].Width = (grdResult.Width * 60) / 100;
                        
                        ControlState(true);
                        break;
                }
            });
        }
        void ControlState(bool state)
        {
            txtSearch.Enabled = state;
            grdResult.Enabled = state;
            btnSearch.Enabled = state;
            pnlNav.Enabled = state;
            if (!state)
            {
                pnlMsg.Visible = true;
            }
            else
            {
                pnlMsg.Visible = false;
            }
        }



        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlForm.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlForm; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
        }

        //---------------------------------------//
        //      Printing Method                  //
        //---------------------------------------//
        int pp = 1;
        int cnt = 0;

        void PrintInventory()
        {
            PrintDocument pd = new PrintDocument();
            pd.DocumentName = lblText.Text;
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            PrintDialog pdlg = new PrintDialog();
            pdlg.Document = pd;
            if (pdlg.ShowDialog() == DialogResult.OK)
            {
                PrintPreviewDialog ppdlg = new PrintPreviewDialog();
                ppdlg.Document = pd;
                //Tracking.EnterLog("Printed Inventory" + txtDR.Text, DateTime.Now.ToString());
                ppdlg.ShowDialog();
            }
        }
        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            float lpp = 0;
            float y = 0;
            float tm = e.MarginBounds.Top;
            float lm = e.PageBounds.Left + 50;
            float lw = e.PageBounds.Width - 50;
            float th = e.PageBounds.Height - 100;
            float tHt = 0;
            Font cFont;
            StringFormat sf = new StringFormat();
            
            float rm = e.PageBounds.Width - 100;
            float bm = e.PageBounds.Height - 100;
            float ht = 0;
            float tmpStx = 0;
            
            //Title
            cFont = new Font("Arial", 16f, FontStyle.Bold);
            tHt = cFont.GetHeight(e.Graphics);
            y = 50;
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString("MBB ROYALE INT'L INC.", cFont, Brushes.Black, (lw + 100) / 2, y, sf);
            //SubTitle
            y += tHt;
            cFont = new Font("Arial", 12f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            e.Graphics.DrawString("INVENTORY", cFont, Brushes.Black, (lw + 100) / 2, y, sf);

            y += tHt;
            cFont = new Font("Arial", 10f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            e.Graphics.DrawString("STORE NAME: "+Common.lStoreName, cFont, Brushes.Black, (lw + 100) / 2, y, sf);
            //Line
            y += tHt;
            y += tHt;
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y), new PointF(lw, y));
            //First Line

            cFont = new Font("Arial Narrow", 10f, FontStyle.Bold);
            //Table Columns
            y += (tHt * 2);
            e.Graphics.DrawString("Barcode", cFont, Brushes.Black, lm + 5, y, new StringFormat());
            e.Graphics.DrawString("Description", cFont, Brushes.Black, lm + 110, y, new StringFormat());
            e.Graphics.DrawString("Whole Stock", cFont, Brushes.Black, lm + 400, y, new StringFormat());
            e.Graphics.DrawString("Retail Stock", cFont, Brushes.Black, lm + 490, y, new StringFormat());
            e.Graphics.DrawString("P. Code", cFont, Brushes.Black, lm + 580, y, new StringFormat());
            e.Graphics.DrawString("Amount", cFont, Brushes.Black, lm + 670, y, new StringFormat());
            
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y - (tHt / 2)), new PointF(lw, y - (tHt / 2)));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y + tHt), new PointF(lw, y + tHt));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y - (tHt / 2)), new PointF(lm, y + tHt));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lw, y - (tHt / 2)), new PointF(lw, y + tHt));
            
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 105, y - (tHt / 2)), new PointF(lm + 105, y + tHt));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 395, y - (tHt / 2)), new PointF(lm + 395, y + tHt));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 485, y - (tHt / 2)), new PointF(lm + 485, y + tHt));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 575, y - (tHt / 2)), new PointF(lm + 575, y + tHt));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 665, y - (tHt / 2)), new PointF(lm + 665, y + tHt));
                        
            //Table
            y += (tHt * 1.5f);
            cFont = new Font("Arial Narrow", 11f, FontStyle.Regular);
            ht = cFont.GetHeight(e.Graphics);
            tHt = ht = cFont.GetHeight(e.Graphics);
            lpp = (bm - y) / ht;
            int cl = 0;
            float stx = 0; 
            
            while (cl <= lpp && cnt < grdResult.Rows.Count)
            {
                sf.Alignment = StringAlignment.Near;
                stx = lm + 1;

                string str = grdResult.Rows[cnt].Cells["fBarcode"].Value.ToString();
                if (str.Length > 10)
                    str = str.Substring(0, 10) + "...";
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);

                str = grdResult.Rows[cnt].Cells["fDescription"].Value.ToString();
                if (str.Length > 34)
                    str = str.Substring(0, 34) + "...";
                stx += 105;
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);
                
                sf.Alignment = StringAlignment.Far;
                stx += 375;
                str = grdResult.Rows[cnt].Cells["fStockWhole"].Value.ToString();
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);

                stx += 470;
                str = grdResult.Rows[cnt].Cells["fStockRetail"].Value.ToString();
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);

                stx += 560;
                str = grdResult.Rows[cnt].Cells["fPCode"].Value.ToString();
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);

                stx += 650;
                str = grdResult.Rows[cnt].Cells["fAmount"].Value.ToString();
                e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);

                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y + tHt), new PointF(lw, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y - (tHt / 2)), new PointF(lm, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lw, y - (tHt / 2)), new PointF(lw, y + tHt));

                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 105, y - (tHt / 2)), new PointF(lm + 105, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 395, y - (tHt / 2)), new PointF(lm + 395, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 485, y - (tHt / 2)), new PointF(lm + 485, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 575, y - (tHt / 2)), new PointF(lm + 575, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 665, y - (tHt / 2)), new PointF(lm + 665, y + tHt));
                
                y += ht;
                cnt++;
                cl++;
            }
                       

            if (cnt < grdResult.Rows.Count)
            {
                e.HasMorePages = true;
                pp++;
            }
            else
            {
                e.HasMorePages = false;
                cnt = 0;
                pp = 1;
            }
             
            /*
            cFont = new Font("Courier New", 12f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            float amt = 0f;
            for (int i = 0; i < items.Rows.Count; i++)
            {
                sf.Alignment = StringAlignment.Center;
                e.Graphics.DrawString(items.Rows[i]["Qty"].ToString(), cFont, Brushes.Black, lm + 32, y, sf);
                e.Graphics.DrawString(items.Rows[i]["Barcode"].ToString(), cFont, Brushes.Black, lm + 70, y, new StringFormat());
                string str = items.Rows[i]["Description"].ToString();
                if (str.Length > 25) str = str.Substring(0, 22) + "...";
                e.Graphics.DrawString(str, cFont, Brushes.Black, lm + 200, y, new StringFormat());
                sf.Alignment = StringAlignment.Far;
                e.Graphics.DrawString(items.Rows[i]["P.Code"].ToString(), cFont, Brushes.Black, lm + 625, y, sf);
                amt = float.Parse(items.Rows[i]["Qty"].ToString()) * float.Parse(UnitPrice.Rows[i]["UnitPrice"].ToString());
                e.Graphics.DrawString(General.FormatCurrency(amt.ToString()), cFont, Brushes.Black, lw - 5, y, sf);
                y += (tHt * 1.25f);
            }
            cFont = new Font("Courier New", 12f, FontStyle.Bold);
            tHt = cFont.GetHeight(e.Graphics);
            y = th - 200 - (tHt * 1.25f);
            e.Graphics.DrawString("TOTAL", cFont, Brushes.Black, lm + 550, y, new StringFormat());
            e.Graphics.DrawString(txtTotal.Text, cFont, Brushes.Black, lw - 5, y, sf);
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, th - 200 - (tHt * 1.5f)), new PointF(lw, th - 200 - (tHt * 1.5f)));
            //Footer
            cFont = new Font("Arial", 12f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            y = th - 200 + 50;
            sf.Alignment = StringAlignment.Near;
            e.Graphics.DrawString("All merchandise covered by this delivery receipt has been", cFont, Brushes.Black, lm + 5, y, sf);
            e.Graphics.DrawString("Received in good order and condition", cFont, Brushes.Black, lw - 250, y, sf);

            y += tHt;
            e.Graphics.DrawString("verified and checked by receiver in good condition.", cFont, Brushes.Black, lm + 5, y, sf);
            y += tHt;
            e.Graphics.DrawString("_____________________________", cFont, Brushes.Black, lw - 250, y, sf);

            y += tHt;
            cFont = new Font("Arial", 8f, FontStyle.Bold);
            tHt = cFont.GetHeight(e.Graphics);
            e.Graphics.DrawString("AUTHORIZED SIGNATURE", cFont, Brushes.Black, lw - 190, y, sf);
            */
        }

    }
}
